﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Compilex.CodeGeneration
{
    internal class CallStack
    {
        private CodeGenerator Generator { get; set; }
        private CommonOperations CommonOps { get { return Generator.Ops; } }
        private StringBuilder customBuilder = null;
        private StringBuilder CodeBuilder
        {
            get
            {
                return customBuilder == null ? Generator.CodeBuilder : customBuilder;
            }
        }
        internal const string StackBase = "/FFE";
        
        internal CallStack(CodeGenerator generator)
        {
            this.Generator = generator;
        }

        internal CallStack(CodeGenerator generator, StringBuilder builder)
        {
            this.Generator = generator;
            this.customBuilder = builder;
        }

        internal void PushWord(string label)
        {
            PushWord(label, 2);   
        }

        internal void IncrementStack(int size)
        {
            CommonOperations ops = new CommonOperations(Generator, CodeBuilder);
            ops.Sub("SP", size);
            ops.StoreTo("SP");
        }

        internal void PushWord(string label, int size)
        {
            CommonOperations ops = new CommonOperations(Generator, CodeBuilder);
            ops.Comment("push " + label);
            IncrementStack(size);
            ops.MoveToPointer("SP", label);
            ops.Comment("fim push " + label);
        }

        internal void PushAddress(string label, int size)
        {
            CommonOperations ops = new CommonOperations(Generator, CodeBuilder);
            ops.Comment("push do endereco de " + label);
            IncrementStack(size);
            ops.MoveAddressToPointer("SP", label);
            ops.Comment("fim push do endereco de " + label);
        }


       
    }
}
